import com.dinmax.flash.draw.control.text.*

class TextControl extends Text {
	private var viewText:TextView;
	private var base:MovieClip;
	private var textField:TextField;
	private var listener:Object;
		
	function TextControl(container:MovieClip, name:String, view:TextView){
	super(container, name);
		if(view==undefined){
		viewText=new TextViewClassic();
		} else {
		viewText=view;
		}

	listener=new Object();
	listener.root=this;
		listener.onChanged=function(txt){
		this.root.changed();
		this.root.value=this.root.textField.text;
		}
	}
	
	public function getViewText():TextView{
	return viewText;
	}
	public function setViewText(viewText:TextView){
	this.viewText=viewText;
	}

	public function setValue(value:String):Void{
	this.value=value;
	textField.htmlText=value;
	changed();
	}

	public function changed():Void{
	textField.setTextFormat(viewText.getTextViewFormat().getTextFormat());
	propagateChangeEvent();
	}
	
	public function draw(){
	reDraw();
	}
	
	public function reDraw(){
	//borro el value
	value=undefined;
	//borro y vuelvo a crear el movieClip
	removeMovieClip(base);
	base=createMovieClip();
	base._x=getLocation().getX();
	base._y=getLocation().getY();
	//dibujo el campo de texto
	base.createTextField("baseText", base.getNextHighestDepth(), 0,0, getSize().getX(), getSize().getY());
	textField=base.baseText;
	viewText.formatField(textField);
	textField.addListener(listener);
	}

	function getTextField():TextField{
	return textField;
	}

}